﻿using AutoMapper;
using MediatR;
using SlaughterItem.Common.Domains.Detection;
using SlaughterItem.Common.Dtos;
using SlaughterItem.Common.Infrastructures.Interfaces;
using SlaughterItem.Enum;

namespace SlaughterItem.API.Application.Commands.Week02._IPQC.AddIPQCData.AddIPQCGoods
{
    public class AddIPQCGoodsHandler : IRequestHandler<AddIPQCGoodsCommand, Result>
    {
        //添加检验标准商品表数据
        public AddIPQCGoodsHandler(IBaseRepository<IPQCGoodsData> iPQCGoodsDataRepository, IMapper mapper)
        {
            IPQCGoodsDataRepository = iPQCGoodsDataRepository;
            Mapper = mapper;
        }

        public IBaseRepository<IPQCGoodsData> IPQCGoodsDataRepository { get; set; }
        public IMapper Mapper { get; set; }
        public async Task<Result> Handle(AddIPQCGoodsCommand request, CancellationToken cancellationToken)
        {
            var result = new Result() {Code=(int)CommonErrorCode.Added };
            int AddNum = 0;
            for (var i = 0; i <request.GoodsId.Count; i++)
            {
                IPQCGoodsData iPQCGoodsData = new IPQCGoodsData()
                {
                    IPQCGoodsId=0,
                    IPQCId=request.IPQCId,
                    GoodsId = request.GoodsId[i],
                    GoodsName=request.GoodsName[i],
                    IsiPQCGoods=request.IsiPQCGoods
                };
                AddNum += IPQCGoodsDataRepository.Add(iPQCGoodsData);
            }
            if (AddNum > 0)
            {
                result.Code = (int)CommonErrorCode.Success;
                return result;
            }
            else
            { 
                return result;
            }
        }
    }
}
